package com.ciwong.epaper.modules.me.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.me.ui.OfflineCatalogActivity;
import com.ciwong.epaper.util.download.DownLoad;
import com.ciwong.epaper.util.download.DownLoadInfo;
import com.ciwong.libs.utils.CWLog;
import com.ciwong.mobilelib.widget.listview.SlideHolder;
import com.ciwong.mobilelib.widget.listview.SlideListView;
import com.ciwong.mobilelib.widget.listview.SlideView;

/**
 * 
 * 离线文件
 *
 * adapter Created by res-jianpingliu on 2015/7/24.
 */
public class OfflineCatalogAdapter extends BaseAdapter
        implements SlideView.OnSlideClickListener, SlideView.OnSlideListener,
        SlideView.SlideSwitch
{
    private Context mContext;

    private List<DownLoadInfo> downLoadInfos;

    private SlideListView mListView;

    private boolean isEdit;

    public OfflineCatalogAdapter(Context context, List<DownLoadInfo> downLoadInfos,
                                 SlideListView listView)
    {
        this.mContext = context;
        this.mListView = listView;
        this.downLoadInfos = downLoadInfos;
    }

    @Override
    public int getCount()
    {
        if (downLoadInfos != null && !downLoadInfos.isEmpty())
        {
            return downLoadInfos.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position)
    {
        if (downLoadInfos != null && !downLoadInfos.isEmpty())
        {
            return downLoadInfos.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position)
    {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent)
    {
        ViewHolder item;
        SlideView slideView = (SlideView) convertView;
        if (slideView == null)
        {
            View itemView = View.inflate(mContext,
                    R.layout.adapter_offline_file_item, null);

            slideView = new SlideView(mContext);
            slideView.setContentView(itemView);
            slideView.hideFirstView();
            item = new ViewHolder(slideView);
            item.setNeedInflate(false);
            item.slideView = slideView;
            slideView.setTag(item);
        }
        else if (((ViewHolder) convertView.getTag()).isNeedInflate())
        {
            View itemView = View.inflate(mContext,
                    R.layout.adapter_offline_file_item, null);

            slideView = new SlideView(mContext);
            slideView.setContentView(itemView);
            slideView.hideFirstView();
            item = new ViewHolder(slideView);
            item.setNeedInflate(false);
            item.slideView = slideView;
            slideView.setTag(item);
        }
        else
        {
            item = (ViewHolder) slideView.getTag();
        }

        slideView.setSlideSwitch(this);
        slideView.setISlideListView(mListView);
        slideView.shrink();
        slideView.setOnSlideClickListener(this);

        slideView.setSlideItemOnClickListener(
                new SlideView.SlideItemOnClickListener()
                {
                    @Override
                    public void onClick(SlideView slideView, int position)
                    {
                        CWLog.i("onClick-position", position + "");
                        OfflineCatalogActivity activity = (OfflineCatalogActivity) mContext;
                        activity.onItemClick(position);
                    }
                });

        DownLoadInfo info = null;
        try
        {
            info = downLoadInfos.get(position);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            notifyDataSetChanged();
            return convertView;
        }
        final DownLoadInfo tempInfo = info;
        item.titleTv.setText(info.getChapterName());
        item.delCb.setVisibility(isEdit ? View.VISIBLE : View.GONE);
        item.delCb.setChecked(downLoadInfos.get(position).isCheck());
        // if (isCheck)
        // {
        // item.delCb.setChecked(isCheck);
        // checkItem(tempInfo, isCheck);
        // }
        // item.delCb
        // .setOnCheckedChangeListener(new
        // CompoundButton.OnCheckedChangeListener()
        // {
        // @Override
        // public void onCheckedChanged(CompoundButton buttonView,
        // boolean isChecked)
        // {
        // checkItem(tempInfo, isChecked);
        // mList.get(position).setIsCheck(isChecked);
        // }
        // });
        return slideView;
    }

    /**
     * 执行第一个view的点击事件
     *
     * @param position
     *            listview中的下标(已去掉了header的数量)
     * @param v
     */
    @Override
    public void performSecondViewOnClick(int position, View v)
    {
        DownLoadInfo downLoadInfo = downLoadInfos.get(position);
        downLoadInfos.remove(position);
        notifyDataSetChanged();
        List<DownLoadInfo> list = new ArrayList<DownLoadInfo>();
        list.add(downLoadInfo);
        DownLoad.getInstance().deletePackages(list);
    }

    /**
     * 执行第二个view的点击事件
     *
     * @param position
     *            listview中的下标(已去掉了header的数量)
     * @param v
     *            当前的操作的view
     * @param isDerectCollapse
     */
    @Override
    public void performFirstViewOnClick(int position, View v,
            boolean isDerectCollapse)
    {

    }

    /**
     * 滑动状态回调
     *
     * @param view
     *            当前操作的view
     * @param position
     *            当前操作的view在listView中的索引
     * @param status
     */
    @Override
    public void onSlide(SlideView view, int position, int status)
    {

    }

    /**
     * 是否可以滑动
     *
     * @param view
     *            当前操作的view
     * @param position
     *            当前操作的view在listView中的索引
     * @param id
     *            view的id
     * @return true:允许滑动
     */
    @Override
    public boolean isSlideEnable(SlideView view, int position, long id)
    {
        if (!isEdit)
        {
            return true;
        }
        return false;
    }

    private class ViewHolder extends SlideHolder implements SlideView.SlideItem
    {
        TextView titleTv;

        CheckBox delCb;

        private SlideView slideView;

        ViewHolder(View convertView)
        {
            titleTv = (TextView) convertView
                    .findViewById(R.id.offline_file_item_title_tv);
            delCb = (CheckBox) convertView
                    .findViewById(R.id.offline_file_item_cb);
        }

        /**
         * 获得当前操作的view
         *
         * @return 操作的view
         */
        @Override
        public SlideView getSlideView()
        {
            return slideView;
        }
    }

    public void setEdit(boolean isEdit)
    {
        this.isEdit = isEdit;
    }
}
